/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#include <stdio.h>
#include <plc4c/spi/evaluation_helper.h>
#include "mqt_t__string.h"

// Code generated by code-generation. DO NOT EDIT.


// Parse function.
plc4c_return_code plc4c_mqtt_read_write_mqt_t__string_parse(plc4c_spi_read_buffer* readBuffer, plc4c_mqtt_read_write_mqt_t__string** _message) {
  uint16_t startPos = plc4c_spi_read_get_pos(readBuffer);
  plc4c_return_code _res = OK;

  // Allocate enough memory to contain this data structure.
  (*_message) = malloc(sizeof(plc4c_mqtt_read_write_mqt_t__string));
  if(*_message == NULL) {
    return NO_MEMORY;
  }

  // Implicit Field (stringLength) (Used for parsing, but it's value is not stored as it's implicitly given by the objects content)
  uint16_t stringLength = 0;
  _res = plc4c_spi_read_unsigned_short(readBuffer, 16, (uint16_t*) &stringLength);
  if(_res != OK) {
    return _res;
  }

  // Simple Field (value)
  char* value = "";
  _res = plc4c_spi_read_string(readBuffer, -1, "UTF-8", (char**) &value);
  if(_res != OK) {
    return _res;
  }
  (*_message)->value = value;

  return OK;
}

plc4c_return_code plc4c_mqtt_read_write_mqt_t__string_serialize(plc4c_spi_write_buffer* writeBuffer, plc4c_mqtt_read_write_mqt_t__string* _message) {
  plc4c_return_code _res = OK;

  // Implicit Field (stringLength) (Used for parsing, but it's value is not stored as it's implicitly given by the objects content)
  _res = plc4c_spi_write_unsigned_short(writeBuffer, 16, plc4c_spi_evaluation_helper_str_len(_message->value));
  if(_res != OK) {
    return _res;
  }

  // Simple Field (value)
  _res = plc4c_spi_write_string(writeBuffer, -1, "UTF-8", _message->value);
  if(_res != OK) {
    return _res;
  }

  return OK;
}

uint16_t plc4c_mqtt_read_write_mqt_t__string_length_in_bytes(plc4c_mqtt_read_write_mqt_t__string* _message) {
  return plc4c_mqtt_read_write_mqt_t__string_length_in_bits(_message) / 8;
}

uint16_t plc4c_mqtt_read_write_mqt_t__string_length_in_bits(plc4c_mqtt_read_write_mqt_t__string* _message) {
  uint16_t lengthInBits = 0;

  // Implicit Field (stringLength)
  lengthInBits += 16;

  // Simple field (value)
  lengthInBits += -1;

  return lengthInBits;
}

